#include <cxcore.h>
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
void mouth(IplImage*);
int dilated(CvMat* ,int ,int ,unsigned char [7][7]);
int erod(CvMat* ,int ,int,unsigned char [7][7]);
int main(){
    IplImage * resim;
    int i,j;
    cvNamedWindow("My Window", CV_WINDOW_AUTOSIZE);
    double scr=0.0,scrcb=0.0,nu,n,cr,crb;
    CvScalar cm,cm2;

          
    resim = cvLoadImage("lc.jpg",1);
    IplImage* m = cvCreateImage(cvSize(resim->width,resim->height), IPL_DEPTH_8U, 3);

    cvCvtColor(resim,m,CV_BGR2YCrCb);

  //  cvDilate(son,son,NULL,1);
  //  cvThreshold(son, son, 100, 255, CV_THRESH_BINARY);
//    cvDilate(son,son,NULL,1);
  //  cvSaveImage("mouth.jpg",son);
   mouth(m);

     cvReleaseImage(&m);
     cvReleaseImage(&resim);
    system("pause");
    return 0;
}
void mouth(IplImage* m){
          CvScalar cm,cm2;
          int i,j;
          double scr=0.0,scrcb=0.0,nu,n,cr,crb;
          CvMat *mou = cvCreateMat( m->height, m->width, CV_32FC1);
          CvMat *cr2 = cvCreateMat( m->height, m->width, CV_32FC1);
          CvMat *crcb = cvCreateMat( m->height, m->width, CV_32FC1);
          IplImage* son = cvCreateImage(cvSize(m->width,m->height), IPL_DEPTH_8U, 1);
    for(i=0;i<(m->height)-1;i++){
      for(j=0;j<(m->width)-1;j++){
          cm=cvGet2D(m,i,j);                       
          cvmSet(cr2,i,j,pow((double)cm.val[1],2));                          
          cvmSet(crcb,i,j,(double)(cm.val[1]/cm.val[2]));
      }
    }      
    cvNormalize(cr2,cr2,0,255, CV_MINMAX, 0);
    cvNormalize(crcb,crcb,0,255, CV_MINMAX, 0);
          
    for(i=0;i<(m->height)-1;i++){
      for(j=0;j<(m->width)-1;j++){
          scr+=cvmGet(cr2,i,j);
          scrcb+=cvmGet(crcb,i,j);        
      }
    }          
    n=m->height*m->width;
    nu=0.95*((scr/n)/(scrcb/n));
    
    for(i=0;i<(m->height)-1;i++){
      for(j=0;j<(m->width)-1;j++){
       cr=cvmGet(cr2,i,j);
       crb=cvmGet(crcb,i,j);                 
       cvmSet(mou,i,j,cr*pow((cr-(nu*crb)),2));                          
      }
    }                    
    cvNormalize(mou,mou,0,255, CV_MINMAX, 0);            
    for(i=0;i<(m->height)-1;i++){
      for(j=0;j<(m->width)-1;j++){
       cm2.val[0]= cvmGet(mou,i,j);   
       cvSet2D(son,i,j,cm2);                      
      }
    }
    cvDilate(son,son,NULL,1);
    cvThreshold(son, son, 120, 255, CV_THRESH_BINARY);
  //  cvDilate(son,son,NULL,1);
    cvSaveImage("mouth.jpg",son);
    cvNamedWindow("My Window", CV_WINDOW_AUTOSIZE);
   cvShowImage("My Window",son);
    cvWaitKey(20000);
    cvReleaseMat(&mou);
    cvReleaseMat(&cr2);
    cvReleaseMat(&crcb);
}
int dilated(CvMat* y,int i,int j,unsigned char m[7][7]){
    
     int k,l,t=3,temp,max;

			max = 0;
			for(k=0;k<7;k++){
				for(l=0;l<7;l++){
					temp=(int) cvmGet(y,(i-t+k),(j-t+l))* m[k][l];
         
			       if(temp>max)
			   	   max = temp;
              }
            }
  
     return max;
    
   /*  	for(i=0;i<y->height;i++){//maskelenmeyen yerler
			for(j=0;j<t;j++){
                cvmSet(g,i,j,cvmGet(y,i,j)); 
                cvmSet(g,i,(y->height-1)-j,cvmGet(y,i,y->height-1)-j)); 

		     }
         }
		for(i=0;i<t;i++){//maskelenmeyen yerler
			for(j=0;j<y->width;j++){
                cvmSet(g,i,j,cvmGet(y,i,j)); 
                cvmSet(g,(y->width-1)-i,j,cvmGet(y,(y->width-1)-i,j); 

		    }
        }
       for(i = t;i<(y->height-t);i++){
		for(j = t;j<(y->width -t);j++){
              
         }
      }*/
}
int erod(CvMat* y,int i,int j,unsigned char m[7][7]){
      int k,l,t=3,temp,min=255;

			for(k=0;k<7;k++){
				for(l=0;l<7;l++){
					temp=(int) cvmGet(y,(i-t+k),(j-t+l))* m[k][l];
         
			       if(temp<min && temp!=0)
			   	    min = temp;
              }
            }
     return min;
    }
